# coding: utf-8
import pandas as pd
import datetime
import pytz
import pymongo, MySQLdb
import resampleUtils as reutil
from time import time
from threading import Thread

# this is to generate all the weekly and monthly data till today (after 15:00)
t0 = time()
client = pymongo.MongoClient()
source = client.Daily_data.qfq_data
target_weekly = client.Weekly_data.qfq_data
target_monthly = client.Monthly_data.qfq_data


def threadsolve(symbols, thread_i, threadCount):
    session = len(symbols) / threadCount
    session += 1
    start = session * thread_i
    end = session * (thread_i + 1)
    if end < len(symbols):
        DailyUpdate(symbols[start:end], source, target_weekly, target_monthly)
    else:
        DailyUpdate(symbols[start:], source, target_weekly, target_monthly)


def DailyUpdate(symbols, source, target_weekly, target_monthly):
    symbols_to_recal = reutil.stockList_recal(symbols)
    for symbol in symbols:
        #print("operate %s" % symbol)
        if symbol in symbols_to_recal:
            print("%s need to recalculate weekly and monthly data" % symbol)
            target_weekly.remove({"symbol": symbol})
            target_monthly.remove({"symbol": symbol})
            reutil.till_last_week_generation(source, [symbol], target_weekly)
            reutil.till_last_month_generation(source, [symbol], target_monthly)

        reutil.present_month_update(source, [symbol], target_monthly)
        reutil.present_week_update(source, [symbol], target_weekly)
        #print("%s is updated" % symbol)


def _get_stocks_from_db(mysql_config):
    # connect to mysql
    conn = MySQLdb.connect(mysql_config['host'], mysql_config['username'], mysql_config['password'],
                           mysql_config['database'])
    cur = conn.cursor()
    res = []
    cur.execute("select symbol from stock_info where suspension=0")
    for (symbol,) in cur:
        res.append(symbol)
    return res


mysql_config = dict()
mysql_config['host'] = 'rdsqtehrv8tqh7v60yvujpublic.mysql.rds.aliyuncs.com'
mysql_config['username'] = 'tradingfloor'
mysql_config['password'] = 'tradingfloor123'
mysql_config['database'] = 'tradingreason'
symbols = _get_stocks_from_db(mysql_config)
print("%d stocks to update" % len(symbols))
threadCount = 30
threads = []
for i in range(threadCount):
    t = Thread(target=threadsolve, args=(symbols, i, threadCount))
    t.start()
    threads.append(t)
for t in threads:
    t.join()

print("finished in %.2fs" % (time() - t0))
